/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.dal;

import client.entity.Recruitment;
import client.entity.RecruitmentCategory;
import client.entity.RecruitmentLocation;
import java.util.ArrayList;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author mrlucky
 */
public class RecruitmentDAL {

    public RecruitmentDAL() {
    }

    public void insertRecrui(Recruitment rec) {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.save(rec);
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
    
    public void updateRecrui(Recruitment rec) {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.update(rec);
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }

    public void insertReCate(ArrayList<RecruitmentCategory> listrc) {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        for (RecruitmentCategory rc : listrc) {
            ss.save(rc);
        }
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }    
    
    
    public void deleteReCateByRecId(int recId)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.createQuery("delete from RecruitmentCategory rc where rc.recruitment.id ="+recId).executeUpdate();
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
    
    public void deleteReLocByRecId(int recId)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.createQuery("delete from RecruitmentLocation rl where rl.recruitment.id ="+recId).executeUpdate();
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }

    public void insertReLoca(ArrayList<RecruitmentLocation> listrl) {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        for (RecruitmentLocation rl : listrl) {
            ss.save(rl);
        }
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
    

    public ArrayList<Recruitment> loadRecByCustomerID(int cusID) {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ArrayList<Recruitment> list = (ArrayList<Recruitment>) ss.createQuery("from Recruitment r where r.customer.id = " + cusID).list();
        ss.beginTransaction().commit();
        ss.close();
        return list;
    }
    
    public Recruitment loadRecByID(String recID)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        Recruitment r = (Recruitment) ss.createQuery("from Recruitment r where r.id = "+recID).uniqueResult();
        ss.beginTransaction().commit();
        ss.close();
        return r;
    }
    
    public void deleteRec(Recruitment rec)
    {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        ss.beginTransaction().begin();
        ss.delete(rec);
        ss.beginTransaction().commit();
        ss.flush();
        ss.close();
    }
}
